package com.zego.wawaji.base.bean;

import android.os.Parcel;
import android.os.Parcelable;

import com.zego.wawaji.base.data.BaseData;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 升级更新
 * Created by Su on 2017/12/4.
 */
public class Updata extends BaseData implements Parcelable {
    private long verCode;
    private String verName;
    private String appUrl;      // 更新地址
    private int mustUpdate;     // 是否强制升级  1：强制
    private List<UpExplain> explainList = new ArrayList<>();

    @Override
    public void parseJson(String jsonStr) {
        JSONObject jsonObject = getJsonObject(jsonStr);

        if (!jsonObject.isNull("NewVersion")) {
            JSONObject baseObject = getJsonObject(jsonObject.optString("NewVersion"));
            this.verCode = baseObject.optLong("VerCode");
            this.verName = baseObject.optString("VerName");
            this.appUrl = baseObject.optString("AppUrl");
            this.mustUpdate = baseObject.optInt("MustUpdate");
        }

        if (!jsonObject.isNull("RecUpdate")) {
            String json = jsonObject.optString("RecUpdate");
            JSONArray jsonArray = getJsonArray(json);
            this.explainList = (List<UpExplain>) getBaseDataList(jsonArray, UpExplain.class);
        }
    }

    public boolean isMustUpdate() {
        return mustUpdate == 1;
    }

    public long getVerCode() {
        return verCode;
    }

    public String getVerName() {
        return verName;
    }

    public String getAppUrl() {
        return appUrl;
    }

    public List<UpExplain> getExplainList() {
        return explainList;
    }

    public Updata() {
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.verCode);
        dest.writeString(this.verName);
        dest.writeString(this.appUrl);
        dest.writeInt(this.mustUpdate);
        dest.writeTypedList(this.explainList);
    }

    protected Updata(Parcel in) {
        this.verCode = in.readLong();
        this.verName = in.readString();
        this.appUrl = in.readString();
        this.mustUpdate = in.readInt();
        this.explainList = in.createTypedArrayList(UpExplain.CREATOR);
    }

    public static final Creator<Updata> CREATOR = new Creator<Updata>() {
        @Override
        public Updata createFromParcel(Parcel source) {
            return new Updata(source);
        }

        @Override
        public Updata[] newArray(int size) {
            return new Updata[size];
        }
    };
}
